package com.jackstraw.jack_base.mq.rocketmq.bean;

import com.jackstraw.jack_base.mq.common.entity.MqMessage;
import com.jackstraw.jack_base.util.ObjectUtil;
import com.jackstraw.jack_base.util.StringUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.messaging.core.MessagePostProcessor;


import java.util.Map;

@Data
@Accessors(chain = true)
public class RocketMqMessage<M> extends MqMessage<M> {
    /**
     * 消息的TAG
     */
    private String tags;
    /**
     * 消息的主键
     */
    private String key;

    /**
     * 消息回调
     */
    private SendCallback sendCallback;

    /**
     * 使用此键选择队列。 例如:orderId, productId…
     */
    private String hashKey;


    /**
     * 事务消息所需的arg
     */
    private Object arg;

    /**
     * 要应用于消息的后处理程序
     */
    private MessagePostProcessor postProcessor;

    /**
     * topic:tags
     */
    private String destination;


    public String getTransactionId() {
        return StringUtil.StringUtilsBylang3.isEmpty(super.getTransactionId()) ?
                (ObjectUtil.ObjectUtilByLang3.isEmpty(this.getHeaders().get(RocketMQHeaders.TRANSACTION_ID)) ? ""
                        : String.valueOf(this.getHeaders().get(RocketMQHeaders.TRANSACTION_ID)))
                : super.getTransactionId();
    }

    public RocketMqMessage<M> setTransactionId(String transactionId) {
        super.setTransactionId(transactionId);
        this.getHeaders().put(RocketMQHeaders.TRANSACTION_ID, transactionId);
        return this;
    }


    public String getTags() {
        return StringUtil.StringUtilsBylang3.isEmpty(this.tags) ? "" : this.tags;
    }

    public String getDestination() {
        return StringUtil.StringUtilsBylang3.isEmpty(destination) ? this.getTopic().concat(":").concat(this.getTags()) : this.destination;
    }

    public RocketMqMessage<M> setDestination(String destination) {
        String[] tempArr = destination.split(":", 2);
        this.setTopic(tempArr[0]);
        this.setTags("");
        if (tempArr.length > 1) {
            this.setTags(tempArr[1]);
        }
        this.destination = destination;
        return this;
    }

    public RocketMqMessage<M> setDestination(String topic, String tags) {
        this.setTopic(topic);
        this.setTags(tags);
        this.destination = this.getTopic().concat(":").concat(this.getTags());
        return this;
    }

    @Override
    public RocketMqMessage<M> setHeaders(String headerName, String headerValue) {
        super.setHeaders(headerName, headerValue);
        if (RocketMQHeaders.TRANSACTION_ID.equals(headerName)) {
            super.setTopic(headerName);
        }
        return this;
    }

    public RocketMqMessage() {
    }

    public RocketMqMessage(String tenantName) {
        super(tenantName);
    }

    public RocketMqMessage(String topic, M message) {
        this.setTopic(topic);
        this.setMessage(message);
    }

    public RocketMqMessage(String topic, String tags, M message) {
        this.setTopic(topic);
        this.setTags(tags);
        this.setMessage(message);
    }


    public static <M> RocketMqMessage<M> buildJackMqMessage(Class<M> mClass, String topic, M message) {
        return new RocketMqMessage<>(topic, message);
    }

    public static <M> RocketMqMessage<M> buildJackMqMessage(Class<M> mClass, String topic, String tags, M message) {
        return new RocketMqMessage<>(topic, tags, message);
    }


}
